INS
;
;
; CDI.PART2
;
;
; Handle DOS RUN command
;
DORUN    LDA WHCBASIC
         BEQ >1
         STA RUNINTRC
^1       JSR DOLOAD
DORUN.1  JSR CRLF
         JSR INITPTRS
         JMP (RUNADR)
;
;
; If integer BASIC, handle RUN
; command here.
;
DOSIRUN  LDA INTLOMEM
         STA INTVEND
         LDA INTLOMEM+1
         STA INTVEND+1
         JMP (CHNADR)
;
;
; Handle CHAIN command
;
DOCHAIN  JSR DOLD.2
         JSR CRLF
         JSR INITPTRS
         JMP (CHNADR)
;
;
;
;
; Applesoft CHAIN is simply a RUN
;
DOSARUN  JSR LD665
         STA PROMPT
         STA ASONERR
         JMP LD7D2
DOSA2RUN JSR LE65
         STA PROMPT
         STA ASONERR
         JMP LFD4
;
;
; Handle the DOS write command
;
DOWRITE  JSR RWCOMMON
         LDA #5
         STA CSWSTATE
         JMP SCNXIT
;
; DOS READ command
;
DOREAD   JSR RWCOMMON
         LDA #1
         STA CURSTAT
         JMP SCNXIT
;
; Code common to both read and
; write.
;
;
; First, see if a buffer is
; already allocated to the
; file.
;
RWCOMMON JSR LOCBUF
         BCC >2
;
; If not, open a file
;
         JSR DOOPEN
         JMP >3
;
; If so, copy file buffer workspc
; to the file manager workspace
;
^2       JSR COPYPTRS
;
; check to see if "R" or "B"
; parameters were specified.
;
^3       LDA KYWRDFND
         AND #6
         BEQ RTS2
;
; If so, copy numeric operands
; to the parameter list.
;
         LDX #3
^4       LDA RECVAL,X
         STA RECNUM,X
         DEX 
         BPL <4
;
; Because R or B parameter was
; specified, a POSITION command
; must be issued before the
; read or write command.
;
         LDA #$A
         STA FMOP
         JSR FMDRVR
RTS2     RTS 
;
;
; INIT command
;
;
; If "V" parameter not specified,
; or it was specified as 0,
; default to 254
;
DOINIT   LDA #$40
         AND KYWRDFND
         BEQ >5
         LDA VOLVAL
         BNE >6
^5       LDA #254
         STA VOLVAL
;
; Get page # of DOS ($9D for
; 48K) and store in subcode
; field.
;
^6       LDA LOADADR+1
         STA FMSUBOP
;
; Do the INIT.
;
         LDA #$B
         JSR CMDHNDL2
         JMP DOSAVE
;
;
; Catalog function handler
;
DOCAT    LDA #6
         JSR CMDHNDL2
;
; Set default volume to the
; volume number of the disk
; just cataloged.
;
         LDA VOLUME
         STA VOLVAL
         RTS 
;
;
; Change to Applesoft BASIC
;
;
; Try to select ROM A/S
;
DOFP     LDA #$4C
         JSR SETBASIC
         BEQ GOODFP
;
; Set up for integer BASIC
;
         LDA #0
         STA WHCBASIC
;
; Attempt to load in RAM
; applesoft
;
         LDY #$1E
         JSR BLKN2
         LDX #9
^7       LDA LAAB7-1,X
         STA MONVAL,X
         DEX 
         BNE <7
         LDA #$C0
         STA CURSTAT
;
; RUN APPLESOFT, if not present
; on the disk you get the
; FILE NOT FOUND message.
; (when DOS restarts the RUN PGM
; command will be finished).
;
         JMP DORUN
;
;
; Change to integer BASIC
;
DOINT    LDA #$20
         JSR SETBASIC
         BEQ >8
;
; If it ain't thar, tuff luck 
;
         LDA #1
         JMP DOERROR
;
; If it is there, restart DOS
;:
^8       LDA #0
         STA RUNINTRC
GOODFP   JMP DOSSTRT
;
; Set the BASIC according to
; byte passed in acc
;
; Integer -  acc=$20
; APLSOFT -  acc=$4C
;
SETBASIC CMP BASCLD
         BEQ RTS3
         STA RAM
         CMP BASCLD
         BEQ RTS3
         STA ROM
         CMP BASCLD
RTS3     RTS 
;
;
; Handle DOS' EXEC command
;
DOEXEC   JSR DOOPEN
         LDA BUFADR
         STA EXCBUF
         LDA BUFADR+1
         STA EXCBUF+1
         LDA FNAME
         STA EXFLG
         BNE >2              ;handle possible "R" parameter
;
;
; Handle DOS' position command
;
DOPSTION JSR LOCBUF
         BCC >1
         JSR DOOPEN
         JMP >2
^1       JSR COPYPTRS
;
; check for "R" parameter
;
^2       LDA KYWRDFND
         AND #4
         BEQ RTS5
;
;
; search for the record specified
; by the "R" parameter
;
TSTRLOOP LDA RECVAL
         BNE >3
         LDX RECVAL+1
         BEQ RTS5
         DEC RECVAL+1
^3       DEC RECVAL
^4       JSR RDTEXT
         BEQ RD.2
         CMP #$8D
         BNE <4
         BEQ TSTRLOOP
RTS5     RTS 
;
;
; Write a byte to a file
;
;
; Check to see if BASIC is
; still running, close the
; file if the user's program
; stopped without closing the
; file.
;
WRTBYTE  JSR ISBASRUN
         BCS CLSWARM
;
; Get the character and output
; it to the specified file.
;
         LDA ASAVE
         STA DATARANG
         LDA #4
         STA FMOP
         LDA #1
         STA FMSUBOP
         JMP FMDRVR
;
; Read a byte from a file
;
;
; Once again, make sure the
; program is still running.
; Close the file if it isn't
;
READBYTE JSR ISBASRUN
         BCS CLSWARM
;
; set up for state six (skip
; output of prompt character)
;
         LDA #6
RDB.1    STA CSWSTATE
;
; Read a character from the file
;
         JSR RDTEXT
         BNE TSTLWR
;
; If EOF, close file.
; If not in state 3 (EXEC) issue
; END of DATA message.
;
         JSR CLOSFREE
         LDA #3
         CMP CSWSTATE
         BEQ RTS5
RD.2     LDA #5
         JMP DOERROR
;
; If character was a lowercase
; character, strip its H.O.
; bit so the Apple Monitor doesn't
; convert it to upper case.
;
TSTLWR   CMP #$E0
         BCC >5
         AND #$7F
^5       STA ASAVE
         LDX XSAVE
         BEQ >6
;
; In the event the last character
; was a lower case character (and
; hence in the range $60-$7F),
; force the H.O. bit on.
;
         DEX 
         LDA IN,X
         ORA #$80
         STA IN,X
^6       JMP DOSXIT
;
;
; Check to see if BASIC is
; executing
;
ISBASRUN PHA 
;
; See if Integer or Applesoft
; is running.
;
         LDA WHCBASIC
         BEQ >8
;
;
; A/S is running, first see if the
; linenumber's H.O. byte is $FF
; (not running).
;
         LDX ASRUN
         INX 
         BEQ NOTRUNNG
;
; Now see if the prompt is "]"
; (used only in immediate mode).
;
         LDX PROMPT
         CPX #"]"
         BEQ NOTRUNNG
;
ISRUNING PLA 
         CLC 
         RTS 
;
; If integer BASIC is active,
; check the integer BASIC runmode
; flag (negative if running).
;
^8       LDA INTRUN
         BMI ISRUNING
NOTRUNNG PLA 
         SEC 
         RTS 
;
; CLSWARM- closes all files and
; warmstarts DOS.
;
CLSWARM  JSR CLOSFREE
         JSR RSET0
         JMP DOSXIT
;
;
; Read a character from the EXEC
; file buffer.
;
EXECRD   JSR PNTEXEC
         JSR COPYPTRS
         LDA #3
         BNE RDB.1
;
;
; Read a character from a text
; file.
;
RDTEXT   LDA #3
         STA FMOP
         LDA #1
         STA FMSUBOP
         JSR FMDRVR
         LDA DATARANG
         RTS 
;
;
; set up pointers for exec file
;
PNTEXEC  LDA EXCBUF+1
         STA FILEBUF+1
         LDA EXCBUF
         STA FILEBUF
         RTS 
;
;
; File Manager driver routine
;
FMDRVR   JSR FMGR
         BCC RTS10
         LDA RTNCODE
         CMP #5
         BEQ >9
         JMP APNDPTCH
^9       JMP APTCH3
         NOP 
         NOP 
         NOP 
         NOP 
         LDX #0
         STX DATARANG
RTS10    RTS 
;
;
; Misc error messages
;
;
; SYNTAX ERROR
;
CSYNERR  LDA #$B
         BNE DOERROR
;
;
; NO BUFFERS AVAILABLE
;
NOBUF    LDA #$C
         BNE DOERROR
;
;
; PROGRAM TOO LARGE
;
PTOOBIG  LDA #$E
         BNE DOERROR
;
;
; FILE TYPE MISMATCH
;
FMISMTCH LDA #$D
;
;
; GENERAL PRINT ERROR ROUTINE
;
DOERROR  STA ASAVE
         JSR SETWARM
         LDA WHCBASIC
         BEQ >1
;
; If running under Applesoft,
; check the ONERR flag before
; printing a nasty error
; message.
;
         LDA ASONERR
         BMI >2
;
; If integer BASIC, or A/S w/o
; the ONERR condition, print
; an error message.
;
^1       LDX #0
         JSR PRTERROR
         LDX ASAVE
         JSR PRTERROR
         JSR CRLF
;
; Reset BASIC pointers (just in
; case), if BASIC is running jump
; the the error handler.
;
;  If BASIC has stopped running do
; a warmstart.
;
^2       JSR INITPTRS
         JSR ISBASRUN
         LDX ASAVE
         LDA #3
         BCS >3
         JMP (BASERR)
^3       JMP (BASWARM)
;
; Print the error message
; specifed by the X-register.
;
;
PRTERROR LDA EMSGOFFS,X
         TAX 
^4       STX TEMP1
         LDA ERRMSGS,X
         PHA 
         ORA #$80
         JSR COUT
         LDX TEMP1
         INX 
         PLA 
         BPL <4
         RTS 
;
;
; COPYPARM- COPY PARAMETERS TO
; FILE MANAGER PARAMETER LIST
;
COPYPARM LDA VOLVAL
         STA VOLUME
         LDA DRVAL
         STA DRIVE
         LDA SLOTVAL
         STA SLOT
         LDA PFNADR
         STA FNADR
         LDA PFNADR+1
         STA FNADR+1
         LDA FILEBUF
         STA BUFADR
         LDA FILEBUF+1
         STA BUFADR+1
         RTS 
;
;
; COPYFNAM- copies filename to
; file buffer
;
COPYFNAM LDY #$1D
^5       LDA FNAME,Y
         STA (FILEBUF),Y
         DEY 
         BPL <5
         RTS 
;
;
; COPYPTRS- copies FM workspace
; to file buffer.
;
COPYPTRS LDY #$1E
^6       LDA (FILEBUF),Y
         STA LB5A9,Y
         INY 
         CPY #$26
         BNE <6
         RTS 
;
;
; RSET0- reset to CSWSTATE 0, sets
; the warmstart flag.
;
RSET0    LDY #0
         STY CURSTAT
         STY CSWSTATE
         RTS 
;
;
; LOCBUF- locates an available
; file buffer.
; Or it locates a file buffer
; that has the save filename
; as the file being opened.
;
;
LOCBUF   LDA #0
         STA OPRND+1
;
; go to the first file buffer
; and see if it is available.
;
         JSR FRSTBUF
         JMP CHKZERO
;
; Locate each successive buffer
; in the chain and see if if
; is available.
;
NXTBUF   JSR PNTNXTBF
         BEQ NOTINUSE
;
; If the buffer is in use, compare
; the filenames to see if they
; match.
;
CHKZERO  JSR GFBFNAM
         BNE >7
;
; If buffer is free, use this
; file buffer.
;
;
         LDA FILEBUF
         STA OPRND
         LDA FILEBUF+1
         STA OPRND+1
         BNE NXTBUF          ;always taken
;
;
; Compare file names
;
;
;
^7       LDY #$1D
^8       LDA (FILEBUF),Y
         CMP FNAME,Y
         BNE NXTBUF
         DEY 
         BPL <8
         CLC 
         RTS 
NOTINUSE SEC 
         RTS 
;
;
; Point DBUFP at first buffer in
; chain.
;
FRSTBUF  LDA DBUFP
         LDX DBUFP+1
         BNE >9
;
; Point DBUFP at next buffer in
; the chain.
;
PNTNXTBF LDY #$25
         LDA (FILEBUF),Y
         BEQ RTS7
         TAX 
         DEY 
         LDA (FILEBUF),Y
^9       STX FILEBUF+1
         STA FILEBUF
         TXA 
RTS7     RTS 
;
;
; GFBFNAM- gets first character of
; file buffers filename
;
GFBFNAM  LDY #0
         LDA (FILEBUF),Y
         RTS 
;
;
; See if this buffer is being used
; by an EXEC command.
;
ISEXCBUF LDA EXFLG
         BEQ >0
         LDA EXCBUF
         CMP FILEBUF
         BNE RTS8
         LDA EXCBUF+1
         CMP FILEBUF+1
         BEQ RTS8
^0       DEX 
RTS8     RTS 
;
;
; Check the filetype;
;
CHKTYPE  EOR FILETYPE
         BEQ RTS9
         AND #$7F
         BEQ RTS9
         JSR DOCLOSE
         JMP FMISMTCH
RTS9     RTS 
;
;
; INIT the file buffer chain
;
;
; Set up for first file buffer
;
INITBUFS SEC 
         LDA DBUFP
         STA FILEBUF
         LDA DBUFP+1
         STA FILEBUF+1
;
; set up counter for maximum
; number of buffers.
;
         LDA MAXFILES
         STA TEMP1
;
; Begin by zeroing out the file
; name, marking it as freed.
;
FREENAME LDY #0
         TYA 
         STA (FILEBUF),Y
;
; Init pointer to file manager
; work area.
;
         LDY #$1E
         SEC 
         LDA FILEBUF
         SBC #$2D
         STA (FILEBUF),Y
         PHA 
         LDA FILEBUF+1
         SBC #0
         INY 
         STA (FILEBUF),Y
;
; set up pointer to T/S list
; buffer area.
;
         TAX 
         DEX 
         PLA 
         PHA 
         INY 
         STA (FILEBUF),Y
         TXA 
         INY 
         STA (FILEBUF),Y
;
; set up pointer to data buffer
; area.
;
         TAX 
         DEX 
         PLA 
         PHA 
         INY 
         STA (FILEBUF),Y
         INY 
         TXA 
         STA (FILEBUF),Y
;
; If this isn't the last file
; buffer, set up pointer to
; the next buffer
;
;
         DEC TEMP1
         BEQ SETLINK0
         TAX 
         PLA 
         SEC 
         SBC #$26
         INY 
         STA (FILEBUF),Y
         PHA 
         TXA 
         SBC #0
         INY 
         STA (FILEBUF),Y
         STA FILEBUF+1
         PLA 
         STA FILEBUF
         JMP FREENAME
;
;
; If at last file buffer, zero
; out the pointer to the next
; buffer and return
;
SETLINK0 PHA 
         LDA #0
         INY 
         STA (FILEBUF),Y
         INY 
         STA (FILEBUF),Y
;
; Set up HIMEM for the current
; BASIC.
;
         LDA WHCBASIC
         BEQ >1
;
; Set up HIMEM for A/S
;
         PLA 
         STA ASHIMEM+1
         STA ASSTRS+1
         PLA 
         STA ASHIMEM
         STA ASSTRS
         RTS 
;
;
; Set up HIMEM for integer.
;
^1       PLA 
         STA INTHIMEM+1
         STA INTSTRT+1
         PLA 
         STA INTHIMEM
         STA INTSTRT
         RTS 
;
;
; INITPTRS is called to restore
; DOS' control over the KSW
; and CSW hooks at locations
; $36 and $38.
;
INITPTRS LDA KSW+1
         CMP DOSKBD+1
         BEQ >2
         STA KSWL+1
         LDA KSW
         STA KSWL
         LDA DOSKBD
         STA KSW
         LDA DOSKBD+1
         STA KSW+1
^2       LDA CSW+1
         CMP DOSVID+1
         BEQ RTS11
         STA CSWL+1
         LDA CSW
         STA CSWL
         LDA DOSVID
         STA CSW
         LDA DOSVID+1
         STA CSW+1
RTS11    RTS 
;
;
;
DOSCMDS  DCI 'INIT'
         DCI 'LOAD'
         DCI 'SAVE'
         DCI 'RUN'
         DCI 'CHAIN'
         DCI 'DELETE'
         DCI 'LOCK'
         DCI 'UNLOCK'
         DCI 'CLOSE'
         DCI 'READ'
         DCI 'EXEC'
         DCI 'WRITE'
         DCI 'POSITION'
         DCI 'OPEN'
         DCI 'APPEND'
         DCI 'RENAME'
         DCI 'CATALOG'
         DCI 'MON'
         DCI 'NOMON'
         DCI 'PR#'
         DCI 'IN#'
         DCI 'MAXFILES'
         DCI 'FP'
         DCI 'INT'
         DCI 'BSAVE'
         DCI 'BLOAD'
         DCI 'BRUN'
         DCI 'VERIFY'
         HEX 00
;
;
;
;
KWRDPRMS HEX 21
         HEX 70A070A170A070207020702070207060
         HEX 00220620742206220423782270307040
         HEX 70408040800800080004004070400021
         HEX 792071207120
         HEX 70
;
;
;
PPARMS   ASC "VDSLRBACIO"
PARMBITS HEX 40201008040201C0A090
KWRANGE  HEX 0000
         HEX FE
         HEX 0001000200010007000100FF7F0000FF
         HEX 7F0000FF7F0000FFFF
;
;
ERRMSGS  HEX 0D078D
         DCI 'LANGUAGE NOT AVAILABLE'
         DCI 'RANGE ERROR'
         DCI 'WRITE PROTECTED'
         DCI 'END OF DATA'
         DCI 'FILE NOT FOUND'
         DCI 'VOLUME MISMATCH'
         DCI 'I/O ERROR'
         DCI 'DISK FULL'
         DCI 'FILE LOCKED'
         DCI 'SYNTAX ERROR'
         DCI 'NO BUFFERS AVAILABLE'
         DCI 'FILE TYPE MISMATCH'
         DCI 'PROGRAM TOO LARGE'
         DCI 'NOT DIRECT COMMAND'
         HEX 8D
;
;
;
EMSGOFFS HEX 0003191924333E4C5B646D788498AABB
;
;
; 
; VARIABLES 
;
;
BUFADR   DFS 2
CURSTAT  DFS 1
CSWSTATE DFS 1
CSWL     DFS 2
KSWL     DFS 2
MAXFILES DFS 2
SSAVE    DFS 1
XSAVE    DFS 1
YSAVE    DFS 1
ASAVE    DFS 1
CMDLNIDX DFS 1
MONFLGS  DFS 1
CMDINDX  DFS 1
LOADLEN  DFS 2
PENDCMD  DFS 1
TEMP1    DFS 1
KYWRDIDX DFS 1
KYWRDFND DFS 1
VOLVAL   DFS 2
DRVAL    DFS 2
SLOTVAL  DFS 2
LENVAL   DFS 2
RECVAL   DFS 2
BYTVAL   DFS 2
ADRVAL   DFS 2
MONVAL   DFS 1
FNAME    DFS 30
SFNAME   DFS 30
MXFLS    HEX 03
CTLD     HEX 84
EXFLG    DFS 1
EXCBUF   DFS 2
WHCBASIC DFS 1
RUNINTRC DFS 1
LAAB7    ASC "APPLESOFT"
;
;
; The following are overlapping
; with the equivalent defs in
; the FILE.MNGR file.
;
LAAC1    ADR $B7E8
LAAC3    ADR $B3BB
LAAC5    ADR $B4BB
LACC7    ADR $C000
         END 

